package com.uifp.gradebook.adaptors;

	import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uifp.gradebook.business_objects.Class;

import com.uifp.gradebook.DataBaseHelper;
import com.uifp.gradebook.R;
import com.uifp.gradebook.ViewSelectedClassActivity;
import com.uifp.gradebook.business_objects.Grade;

	public class GradesArrayAdaptor extends ArrayAdapter<Grade> {
	  private final Context context;
	  private final ArrayList<Grade> gradeList;
	  private Grade grade;
	  private static final int DIALOG_ALERT = 10;
	  
	  public GradesArrayAdaptor(Context context, int layoutResourceId, ArrayList<Grade> grades) {
	    super(context, layoutResourceId, grades);
	    this.context = context;
	    this.gradeList = grades;
	  }

	@Override
	  public View getView(int position, View convertView, ViewGroup parent) {
	    LayoutInflater inflater = (LayoutInflater) context
	        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View rowView = inflater.inflate(R.layout.grade_item, parent, false);
	    
	    this.grade = this.gradeList.get(position);
	    
	    TextView textViewCategory = (TextView) rowView.findViewById(R.id.category);
	    TextView textViewWeight = (TextView) rowView.findViewById(R.id.weight);
	    TextView textViewScore = (TextView) rowView.findViewById(R.id.score);
	    
	    textViewCategory.setText(this.grade.getHeadingString());
	    textViewWeight.setText(String.valueOf(this.grade.getWeight()));
	    textViewScore.setText(String.valueOf(this.grade.getActualPts()));
	    
	    if(this.gradeList.get(position).getActualPts() == 0){
		    textViewScore.setText("100");
	    	textViewScore.setTextColor(Color.YELLOW);
	    }
	    RelativeLayout layout = (RelativeLayout)rowView.findViewById(R.id.ActualGrateItem);
	    layout.setTag(this.gradeList.get(position).getGradeID());
	    layout.setOnClickListener(rowClicked);
	    return rowView;
	  }
	private OnClickListener rowClicked = new OnClickListener() {
        @Override
        public void onClick(View view) {
			int gradeID = Integer.parseInt(view.getTag().toString());

    		final Dialog dialog = new Dialog(context);
    		dialog.setContentView(R.layout.dialog_enter_grade);
    		dialog.setTitle("Enter your grade");
    	
    		
    		Button dialogButton = (Button) dialog.findViewById(R.id.submit_grade);
			TextView tvActual = (TextView)dialog.findViewById(R.id.actual_points);
			tvActual.setText(view.getTag().toString());
    		dialogButton.setOnClickListener(new OnClickListener() {

    			@Override
    			public void onClick(View v) {
    				dialog.dismiss();
    				
    				//attempt to update the grade
    				/*
    				TextView tvActual = (TextView)v.findViewById(R.id.actual_points);
    				TextView tvPossible = (TextView)v.findViewById(R.id.possible_points);
    				int actPts = Integer.parseInt((String) tvActual.getText());
    				int posPts = Integer.parseInt((String) tvPossible.getText());
    				int grade = (int)(actPts/posPts);
    				int gradeID = Integer.parseInt(v.getTag().toString());
    				DataBaseHelper db = new DataBaseHelper(context);
    				db.updateGrade(gradeID, grade);
    				*/
    				}
    			});

    		dialog.show();
        }
    };
	} 